/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.view.template;

import offset.nodes.client.view.SchemaNodeRenderer;
import offset.nodes.client.view.TreeNodeChooserDialog;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.swing.JDialog;
import javax.swing.tree.DefaultMutableTreeNode;
import offset.nodes.client.model.Server;
import offset.nodes.client.virtual.model.GetSchemas;
import offset.nodes.client.model.NodeTypes;
import offset.nodes.client.virtual.model.SchemaNode;
import offset.nodes.client.virtual.model.jcr.QName;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeDefinition;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeReader;

/**
 *
 * @author  Walter Lütgenau
 */
public class TemplateChooserDialog extends TreeNodeChooserDialog {
    NodeTypeDefinition definition = null;
    
    /** Creates new form NodeChooserDialog */
    public TemplateChooserDialog(JDialog parent, boolean modal, NodeTypeDefinition[] definitions) {
        super(parent, modal);
        init(buildTree(definitions));
        setCellRenderer(new SchemaNodeRenderer());
    }
    
    public TemplateChooserDialog(JDialog parent, boolean modal, Server model, QName uriFilter) {
        super(parent, modal);
        init(buildTree(new NodeTypes(getDefinitions(model)).getArray(uriFilter)));
    }
    
    public NodeTypeDefinition[] getDefinitions(Server model) {
        GetSchemas.Request request = new GetSchemas.Request();
        GetSchemas.Response response = null;
        try {
            response = (GetSchemas.Response) model.sendRequest(request);
            InputStream in = new ByteArrayInputStream(response.getSchemaXml().getBytes());
            NodeTypeDefinition[] definitions = NodeTypeReader.read(in);
            
            return definitions;
        } catch( Exception e ) {
            e.printStackTrace();
            return null;
        }
    }
    
    public NodeTypeDefinition getDefinition() {
        DefaultMutableTreeNode selected = getSelectedNode();
        if ( selected == null)
            return null;
        
        SchemaNode schema = (SchemaNode) selected.getUserObject();
        return schema.getNodeType();
    }
    
    DefaultMutableTreeNode buildTree(NodeTypeDefinition[] definitions) {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(new SchemaNode(new QName("", ""), null, null));
        
        for( int i=0; i<definitions.length; i++) {
            root.add(new DefaultMutableTreeNode( new SchemaNode(definitions[i].getName(), null, definitions[i])));
        }
        
        return root;
    }
    
}
